// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'

import store from './store/index'
import router from './router'
import axios from 'axios'
import pubFun from './assets/js/common.js'
import './assets/css/common.css';
import { MessageBox, Indicator, Badge, Header, Button } from 'mint-ui';
import 'lib-flexible/flexible'  //px转为rem
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/dist/css/swiper.css'
import './assets/css/iconfont.css'
import preview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'
import qs from 'qs'
import App from './App'
//设置axios为form-data
let options = {
    fullscreenEl: false
};
Vue.use( preview, options )
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.withCredentials = true  //携带cookie
Vue.use( VueAwesomeSwiper, /* { default global options } */ )
Vue.config.productionTip = false;
Vue.prototype.$axios = axios;
Vue.prototype.pubFun = pubFun;
Vue.component( MessageBox.name, MessageBox );
Vue.component( Badge.name, Badge );
Vue.component( Header.name, Header );
Vue.component( Button.name, Button );
Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$loading = Indicator;
/*Vue.prototype.getViewportSize=function() {
    return {
        width: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
        height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
    };
};*/
/* eslint-disable no-new */


var root = process.env.API_ROOT;
new Vue( {
    el: '#app',
    router,
    store,
    components: { App },
    template: '<App/>',
    created() {
        const _this = this;
        this.$axios.interceptors.request.use(
            request => {
                if (!request.data) {
                    request.data = {}
                }
                if (_this.$store.getters.getToken)
                    request.data.openid = _this.$store.getters.getToken
                request.url = root + request.url
                request.data = qs.stringify( request.data )
                return request
            },
            error => {
                return Promise.reject( error )
            }
        )
        //在 response 拦截器实现
        this.$axios.interceptors.response.use(
            response => {
                let result = _this.pubFun.formatResponse( response );
                if (result.status != 0) {
                    //todo:修改未登录的错误处理
                    if (result.status === -2001) {
                        this.$store.commit( 'LOGOUT' )
                        this.$msgbox.alert( '请先登录', '提示' ).then( action => {
                            if (_this.$route.name != 'Mine')
                                _this.$router.push( {
                                    name: 'Mine',
                                } )
                        } );
                        return Promise.reject( result )
                    } else {
                        console.log( '【Error】', result );
                        return Promise.reject( result )
                    }
                }
                if (this.$store.getters.getToken && result.is_login != 1) {

                    this.$store.commit( 'LOGOUT' )
                }
                return result
            },
            error => {
                if (error.response) {
                    let result = _this.pubFun.formatResponse( error.response );
                    console.log( '【Error】', result );
                    switch (result.status) {
                        /* case 404:
                             router.push( '/Error/Error404' );
                             break;*/
                        case 500:
                            result.msg = '接口出错'
                            break;
                    }
                    return Promise.reject( result )
                }
                return Promise.reject( error )

            }
        )
    }
} )
